package cn.stylefeng.guns.modular.staff.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.staff.entity.Staff;
import cn.stylefeng.guns.modular.staff.enums.StaffExceptionEnum;
import cn.stylefeng.guns.modular.staff.mapper.StaffMapper;
import cn.stylefeng.guns.modular.staff.pojo.request.StaffRequest;
import cn.stylefeng.guns.modular.staff.service.StaffService;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;

import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 业务实现层
 *
 * @author xbz
 * @date 2023/03/31 10:23
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

	@Override
    public void add(StaffRequest staffRequest) {
        Staff staff = new Staff();
        BeanUtil.copyProperties(staffRequest, staff);
        this.save(staff);
    }

    @Override
    public void del(StaffRequest staffRequest) {
        Staff staff = this.queryStaff(staffRequest);
        this.removeById(staff.getId());
    }

    @Override
    public void edit(StaffRequest staffRequest) {
        Staff staff = this.queryStaff(staffRequest);
        BeanUtil.copyProperties(staffRequest, staff);
        this.updateById(staff);
    }

    @Override
    public Staff detail(StaffRequest staffRequest) {
        return this.queryStaff(staffRequest);
    }

    @Override
    public PageResult<Staff> findPage(StaffRequest staffRequest) {
        LambdaQueryWrapper<Staff> wrapper = createWrapper(staffRequest);
        Page<Staff> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Staff> findList(StaffRequest staffRequest) {
        LambdaQueryWrapper<Staff> wrapper = this.createWrapper(staffRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/03/31 10:23
     */
    private Staff queryStaff(StaffRequest staffRequest) {
        Staff staff = this.getById(staffRequest.getId());
        if (ObjectUtil.isEmpty(staff)) {
            throw new ServiceException(StaffExceptionEnum.STAFF_NOT_EXISTED);
        }
        return staff;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/03/31 10:23
     */
    private LambdaQueryWrapper<Staff> createWrapper(StaffRequest staffRequest) {
        LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = staffRequest.getId();
        String name = staffRequest.getName();
        String sex = staffRequest.getSex();
        String birthday = staffRequest.getBirthday();
        String ctime = staffRequest.getCtime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Staff::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), Staff::getName, name);
        queryWrapper.like(ObjectUtil.isNotEmpty(sex), Staff::getSex, sex);
        queryWrapper.eq(ObjectUtil.isNotNull(birthday), Staff::getBirthday, birthday);
        queryWrapper.eq(ObjectUtil.isNotNull(ctime), Staff::getCtime, ctime);

        return queryWrapper;
    }

}